package com.woniu.smart.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.smart.dto.CarDTO;
import com.woniu.smart.dto.VisitorCarDTO;
import com.woniu.smart.entity.Access;
import com.woniu.smart.entity.Car;
import com.woniu.smart.entity.Visitor;
import com.woniu.smart.service.AccessService;
import com.woniu.smart.service.CarService;
import com.woniu.smart.service.VisitorService;
import com.woniu.smart.util.ObjectExcelView;
import com.woniu.smart.util.PageData;
import com.woniu.smart.util.Result;
import com.woniu.smart.vo.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：zhulongxing
 * @date ：Created in 2022/10/20 17:59
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("cars")
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private AccessService accessService;
    @Autowired
    private VisitorService visitorService;
    @PostMapping("selAll")
    public Result selAll(@RequestBody CarDTO carDTO) {;
        List<CarVO> carList = carService.selectAll(carDTO);
        return Result.ok().data("carList", carList);
    }
    @PostMapping("selAllTwo")
    public Result selAllTwo(@RequestBody CarDTO carDTO) {
        List<CarVO> carVos = carService.selectAllTwo(carDTO);
        PageInfo<CarVO> carList = new PageInfo<>(carVos);
        return Result.ok().data("carListTwo", carList).data("total",carList.getTotal());
    }
    @Transactional
    @PostMapping("delByIds")
    public Result delByIds(@RequestBody List<Integer> visitorIds) {
        int i = carService.deleteByIds(visitorIds);
        int deleteBatch = visitorService.deleteBatch(visitorIds);
        int delete = accessService.deleteByIds(visitorIds);
        return Result.ok();
    }
    @Transactional
    @PostMapping("{id}")
    public Result add(@PathVariable("id") Long visitorId,@RequestBody VisitorCarDTO visitorCarDTO) {
        visitorCarDTO.setVisitorId(visitorId);
        int insert = carService.insertSelectiveTwo(visitorCarDTO);
        return Result.ok();
    }

    @PutMapping("")
    public Result update(@RequestBody Car car) {
        int update = carService.updateByPrimaryKey(car);
        return Result.ok();
    }

    @GetMapping("{id}")
    public Result queryById(@PathVariable("id") Long id) {
        Car car = carService.selectByPrimaryKey(id);
        return Result.ok().data("car", car);
    }
    @Transactional
    @DeleteMapping("{visitorId}")
    public Result delete(@PathVariable("visitorId") Long visitorId) {
        int delete = carService.deleteByPrimaryKey(visitorId);
        int i = visitorService.deleteById(visitorId);
        int deleteByPrimaryKey = accessService.deleteByPrimaryKey(visitorId);
        return Result.ok();
    }
    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody CarDTO carDTO) {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("车牌号码"); // 1
            titles.add("车辆型号"); // 2
            titles.add("车主姓名"); // 3
            titles.add("联系方式"); // 4
            titles.add("车辆品牌"); // 5
            titles.add("车辆类型"); // 6
            titles.add("开始时间"); // 7
            titles.add("结束时间"); // 8
            dataMap.put("titles", titles);
            List<CarVO> carOList = carService.selectDownExcel(carDTO);
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < carOList.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", carOList.get(i).getCarNum()); // 1
                vpd.put("var2", carOList.get(i).getCarBrand()); // 2
                vpd.put("var3", carOList.get(i).getVisitorName()); // 3
                vpd.put("var4", carOList.get(i).getVisitorTel()); // 4
                vpd.put("var5", carOList.get(i).getCarModel()); // 5
                vpd.put("var6", carOList.get(i).getCarTypeName()); // 6
                vpd.put("var7", carOList.get(i).getAccessIn()); // 7
                vpd.put("var8", carOList.get(i).getAccessOut()); // 8
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
